﻿var xmlHttpReq;
var intSpeed = 500;
var isPage;

function ItemScrollManually(slidingControlName, CurrentState, SpeedToScroll) {
    $SlidingControl = $("#" + slidingControlName);

    $SlidingControl.stop().scrollTo('li:eq(' + CurrentState + ')', SpeedToScroll, { queue: false, axis: 'x', duration: 3000 });
    //    if (IsVartical == "True") {
    //        sett.$SlidingControl.scrollTo('li:eq(' + CurrentState + ')', intSpeed, { queue: true, axis: 'y', duration: 3000 });

    //        $(window)._scrollable();
    //    }
    //    else {
    //        sett.$SlidingControl.stop().scrollTo('li:eq(' + sett.CurrentState + ')', intSpeed, { queue: false, axis: 'x', duration: 3000 });
    //    }
}

function ItemToScroll(sett) {
    var li = GetLIElemets(sett.$SlidingControl);
    CheckItemCall(sett.CurrentState, sett);
    var intCurrentState = eval(sett.CurrentState);

    if (li.length > intCurrentState) {
        var IsVartical = sett.IsVartical;
        if (IsVartical == "True") {
            sett.$SlidingControl.scrollTo('li:eq(' + sett.CurrentState + ')', intSpeed, { queue: true, axis: 'y', duration: 3000 });

            $(window)._scrollable();
        }
        else {
            sett.$SlidingControl.stop().scrollTo('li:eq(' + sett.CurrentState + ')', intSpeed, { queue: false, axis: 'x', duration: 3000 });
        }

        PagingImageChange(sett);
        var lisobject = GetLIElemets(sett.$SlidingControl);
        var dives = $(lisobject[sett.CurrentState]).children();
        //LoadImage(dives[sett.CurrentState]);
        for (j = 0; j < dives.length; j++) {
            LoadImage(dives[j]);
        }

    }
}


function GetLIElemets($SlidingControl) {
    var v = $SlidingControl;
    var ul = v[0].getElementsByTagName("ul");
    var li = ul[0].getElementsByTagName("li");

    return li;
}

function CheckItemCall(ItemNo, sett) {
    var li = GetLIElemets(sett.$SlidingControl);
    if (li.length <= ItemNo) {
        if (sett.TotalPageCount >= ItemNo)
            getPagingContent(li.length, sett);
    }
}

function createXMLHttpRequest() { try { return new XMLHttpRequest(); } catch (e) { } try { return new ActiveXObject("Msxml2.XMLHTTP"); } catch (e) { } try { return new ActiveXObject("Microsoft.XMLHTTP"); } catch (e) { } alert("XMLHttpRequest not supported"); return null; }

function getPagingContent(CurentHTMLLI, sett) {
    if (document.getElementById(sett.UpdateProgressBar) != null) {
        document.getElementById(sett.UpdateProgressBar).style.display = "block";
    }

    xmlHttpReq = createXMLHttpRequest();
    var CurrentPageName = sett.CurrentPageName.replace("#", "");
    xmlHttpReq.onreadystatechange = function () { getPagingContent_callback(sett, xmlHttpReq); };
    xmlHttpReq.open("get", "/SildingControlService.ashx?PageNo=" + sett.CurrentState + "&PageSkip=" + CurentHTMLLI + "&ControlPath=" + sett.ControlPath + "&CurrenPageHiden=" + CurrentPageName, true);
    xmlHttpReq.setRequestHeader("content-type", "application/x-www-form-urlencoded");
    xmlHttpReq.setRequestHeader("content-type", "application/json; charset=utf-8");

    xmlHttpReq.send(null);
}

function getPagingContent_callback(sett, res) {
    if (xmlHttpReq.readyState == 4 || xmlHttpReq.readyState == 'complete') {
        res = xmlHttpReq.responseText
        if (document.getElementById(sett.UpdateProgressBar) != null) {
            document.getElementById(sett.UpdateProgressBar).style.display = "none";
        }

        var v = sett.$SlidingControl;
        var ul = v[0].getElementsByTagName("ul");

        var vs = ul[0].innerHTML;
        ul[0].innerHTML = res;

        var lisobject = ul.item(0).getElementsByTagName("li");
        var InnerHTMLS = "";
        for (i = 0; i < lisobject.length; i++) {
            var InnerHTML = "";
            var InsertInnerHTML = "";
            var dives = $(lisobject[i]).children();

            var j = 0;

            for (j = 0; j < dives.length; j++) {
                if (i == CurrentState) {
                    LoadImage((dives[CurrentState]));
                    //LoadImage(dives[j]);
                }
                if (sett.IsGrouping == "True") {
                    if (j == sett.GroupingCount) {
                        InnerHTMLS = InnerHTMLS + '<div style="clear: both" ></div>';
                    }

                }
                InnerHTMLS = InnerHTMLS + "<div class=" + dives[j].className + " >";
                InnerHTMLS = InnerHTMLS + dives[j].innerHTML;
                InnerHTMLS = InnerHTMLS + "</div>";
            }
            try {

                lisobject[i].innerHTML = InnerHTMLS;
                InnerHTMLS = "";
            }
            catch (ex) { alert(ex) }
        }

        vs = vs + ul[0].innerHTML;
        ul[0].innerHTML = vs;
        var li = GetLIElemets(sett.$SlidingControl);
        if (li.length > sett.CurrentState) {
            ItemToScroll(sett);
        }
    }
    res = null;
    vs = null;
    ul = null;
}
function LoadImage(objLI) {

    var imges = objLI.getElementsByTagName("img");
    for (f = 0; f < imges.length; f++) {
        var imgsrc = imges[f].getAttribute("dummySrc");
        if (imgsrc != "" && imgsrc != null) {
            imges[f].setAttribute("dummySrc", "");
            imges[f].setAttribute("src", imgsrc);
        }
    }
}


function RequestToScroll($SlidingControl, GroupingCount, IsGrouping, isPaging, IsVartical, PagingInImages, SelectedImage, DisSelectImage, NavigationControl, CurrentState, PreviousState, PageNo, TotalPageCount, ControlPath, PagingInWriten, UpdateProgressBar) {
    if (isPaging == "True") {
        var PageCount = TotalPageCount;
    }

    var prevous = PreviousState.val();
    var current = CurrentState.val();

    if (!isNaN(PageNo)) {
        prevous = current;
        current = PageNo;


    }
    else {
        prevous = current;
        if (PageNo == "next") {
            if (prevous == eval(TotalPageCount) - 1) {
                current = 0;
            }
            else {
                current = eval(prevous) + 1;
            }

        }

        else {
            if (prevous == 0) {
                current = eval(TotalPageCount) - 1;
            }
            else {
                current = eval(prevous) - 1;
            }
        }


    }

    var setting = {
        IsGrouping: IsGrouping,
        GroupingCount: GroupingCount,

        isPaging: isPaging,
        PagingInImages: PagingInImages,

        IsVartical: IsVartical,


        NavigationControl: NavigationControl,
        $SlidingControl: $SlidingControl,
        SelectedImage: SelectedImage,
        DisSelectImage: DisSelectImage,
        PageNo: CurrentState,
        CurrentState: current,
        PreviousState: prevous,
        TotalPageCount: TotalPageCount,
        ControlPath: ControlPath,
        UpdateProgressBar: UpdateProgressBar,
        CurrentPageName: CurrentState.selector

    }

    var PagingWriten = document.getElementById(PagingInWriten);
    if (PagingWriten != null) {
        //  PagingWriten.innerHTML = "Page " + eval(eval(current) + 1) + " of " + TotalPageCount;
        if (TotalPageCount == 0) {
            if (isPage != null) {
                PagingWriten.innerHTML = "0 of " + TotalPageCount;
            }
            else {
                PagingWriten.innerHTML = "Page 0 of " + TotalPageCount;
            }
        }
        else {
            if (isPage != null) {
                PagingWriten.innerHTML = eval(eval(current) + 1) + " of " + TotalPageCount;
            }
            else {
                PagingWriten.innerHTML = "Page " + eval(eval(current) + 1) + " of " + TotalPageCount;
            }
        }
    }
    CurrentState.val(current);
    PreviousState.val(prevous);
    ItemToScroll(setting);
}

function PagingImageChange(pos) {
    if (pos.isPaging == "True") {
        var Nav = pos.NavigationControl;
        var Anchor = Nav.getElementsByTagName("a");
        var SelectedImage = pos.SelectedImage.substr(pos.SelectedImage.lastIndexOf("/") + 1, pos.SelectedImage.length - 1);
        var DISSelectedImage = pos.DisSelectImage.substr(pos.DisSelectImage.lastIndexOf("/") + 1, pos.DisSelectImage.length - 1);
        for (i = 0; i < pos.TotalPageCount; i++) {
            if (pos.PagingInImages == "True") {
                var img = Anchor[i].getElementsByTagName("img");
                var src = "";
                if (img[0].src.indexOf(SelectedImage) > 0)
                    src = img[0].src.replace(SelectedImage, "");
                else
                    src = img[0].src.replace(DISSelectedImage, "");
                if (i == pos.CurrentState)
                    src = src + SelectedImage;
                else
                    src = src + DISSelectedImage;

                img[0].src = src;
            }
            else {
                if (i == pos.CurrentState)
                    Anchor[i].style.color = "#000";
                else {
                    Anchor[i].style.color = "blue";
                }

            }
        }
    }
}


function AddEvent(setting) {
    var Navigation = document.getElementById(setting.NavigationControl);
    var va = setting.CurrentState[0].value;
    if (setting.CurrentState[0].value != '0') {
        var strMoveNext = RequestToScroll(setting.$SlidingControl, setting.GroupingCount, setting.IsGrouping, setting.isPaging, setting.IsVartical, setting.PagingInImages, setting.SelectedImage, setting.DisSelectImage, Navigation, setting.CurrentState, setting.PreviousState, "previous", setting.TotalPageCount, setting.ControlPath, setting.PagingWritenID, setting.ProgressBarID);
    }

    if (document.getElementById(setting.NextPageClientName) != null) {
        document.getElementById(setting.NextPageClientName).onclick = function () {
            RequestToScroll(setting.$SlidingControl, setting.GroupingCount, setting.IsGrouping, setting.isPaging, setting.IsVartical, setting.PagingInImages, setting.SelectedImage, setting.DisSelectImage, Navigation, setting.CurrentState, setting.PreviousState, "next", setting.TotalPageCount, setting.ControlPath, setting.PagingWritenID, setting.ProgressBarID);
            return false;
        };
    }

    if (document.getElementById(setting.PreviousPageClientName) != null) {
        document.getElementById(setting.PreviousPageClientName).onclick = function () {
            RequestToScroll(setting.$SlidingControl, setting.GroupingCount, setting.IsGrouping, setting.isPaging, setting.IsVartical, setting.PagingInImages, setting.SelectedImage, setting.DisSelectImage, Navigation, setting.CurrentState, setting.PreviousState, "previous", setting.TotalPageCount, setting.ControlPath, setting.PagingWritenID, setting.ProgressBarID);

            return false;
        };
    }

    var IsPaging = setting.isPaging;
    if (IsPaging == "True") {
        var aTag = Navigation.getElementsByTagName("a");
        for (i = 0; i < aTag.length; i++) {

            aTag[i].onclick = function () {
                var value = this.attributes["af"].value;
                value = eval(value) - 1;
                RequestToScroll(setting.$SlidingControl, setting.GroupingCount, setting.IsGrouping, setting.isPaging, setting.IsVartical, setting.PagingInImages, setting.SelectedImage, setting.DisSelectImage, Navigation, setting.CurrentState, setting.PreviousState, value, setting.TotalPageCount, setting.ControlPath, setting.PagingWritenID, setting.ProgressBarID);
            };
        }
    }
    var strMoveNext = RequestToScroll(setting.$SlidingControl, setting.GroupingCount, setting.IsGrouping, setting.isPaging, setting.IsVartical, setting.PagingInImages, setting.SelectedImage, setting.DisSelectImage, Navigation, setting.CurrentState, setting.PreviousState, va, setting.TotalPageCount, setting.ControlPath, setting.PagingWritenID, setting.ProgressBarID);
}

function initHTML(sett) {

    PreviousState = 0;
    CurrentState = 0;

    var PagingWriten = document.getElementById(sett.PagingWritenID);
    if (PagingWriten != null) {
        // PagingWriten.innerHTML = "Page 1 of " + sett.TotalPageCount;
        var TotalPageCount = sett.TotalPageCount; //document.getElementById(sett.TotalPageCount);
        if (TotalPageCount == 0) {
            if (isPage != null) {
                PagingWriten.innerHTML = "0 of " + TotalPageCount;
            }
            else {
                PagingWriten.innerHTML = "Page 0 of " + TotalPageCount;
            }
        }
        else {
            if (isPage != null) {
                PagingWriten.innerHTML = "1 of " + TotalPageCount;
            }
            else {
                PagingWriten.innerHTML = "Page 1 of " + TotalPageCount;
            }
        }
    }

    var IsGrouping = sett.IsGrouping;
    var GroupingCount = sett.GroupingCount;
    var v = sett.$SlidingControl;
    var ul = v[0].getElementsByTagName("ul");
    var lisobject = ul.item(0).getElementsByTagName("li");
    var InnerHTMLS = "";
    for (i = 0; i < lisobject.length; i++) {
        var InnerHTML = "";
        var InsertInnerHTML = "";
        var dives = $(lisobject[i]).children();

        var j = 0;

        for (j = 0; j < dives.length; j++) {
            if (i == 0) {
                LoadImage(dives[j]);
            }


            if (IsGrouping == "True") {
                if (j == GroupingCount) {
                    InnerHTMLS = InnerHTMLS + '<div style="clear: both" ></div>';
                }
            }
            InnerHTMLS = InnerHTMLS + "<div class=" + dives[j].className + " >";
            InnerHTMLS = InnerHTMLS + dives[j].innerHTML;
            InnerHTMLS = InnerHTMLS + "</div>";
        }
        try {

            lisobject[i].innerHTML = InnerHTMLS;
            InnerHTMLS = "";

        }
        catch (ex) { alert(ex) }
    }
    AddEvent(sett);



}